package com.oa.lms.apps.dao.absence.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.lms.apps.dao.absence.IAbsenceCategoryDao;
import com.oa.lms.apps.entity.absence.AbsenceCategoryValue;


public class AbsenceCategoryDaoImpl extends HibernateDaoSupport implements
		IAbsenceCategoryDao {
	
	List<AbsenceCategoryValue> list;

	@Override
	public Integer add(AbsenceCategoryValue absenceCategoryValue) {
		if (absenceCategoryValue != null) {
			this.getHibernateTemplate().save(absenceCategoryValue);
		}
		return absenceCategoryValue.getCategoryId();
	}

	@Override
	public void modify(AbsenceCategoryValue absenceCategoryValue) {
		if (absenceCategoryValue != null) {
			this.getHibernateTemplate().update(absenceCategoryValue);
		}

	}

	@Override
	public AbsenceCategoryValue queryByCategoryId(Integer CategoryId) {
		if (CategoryId != null) {
			return (AbsenceCategoryValue) this.getHibernateTemplate().get(AbsenceCategoryValue.class, CategoryId);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AbsenceCategoryValue> queryAbsenceCategory(final AbsenceCategoryValue absenceCategoryValue) {
		String hqlStr = "FROM AbsenceCategoryValue acv WHERE 1=1";
		if (absenceCategoryValue.getCategoryName() != null) {
			hqlStr += " AND acv.sectionName = ?";
		}
		hqlStr += " AND acv.status <> -1";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (absenceCategoryValue.getCategoryName() != null) {
					query.setString(++index, absenceCategoryValue.getCategoryName());
				}
				list = query.list();
				return list;
			}
		});
		return list;
	}

	public List<AbsenceCategoryValue> getList() {
		return list;
	}

	public void setList(List<AbsenceCategoryValue> list) {
		this.list = list;
	}
	
	}


